package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.OrderBean;
import com.project.bean.ShoppBean;
import com.project.mapper.IDetailsMapper;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IShoppMapper;

import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper mapper;
    @Autowired
    private IShoppMapper mapper1;
    @Autowired
    private IDetailsMapper mapper2;

    @Override
    public void add(OrderBean order) {
        //计算购物车总价
        Integer totalPrice = mapper1.computeShopp(order.getUserId());
        order.setTotal(totalPrice);
        //添加订单
        mapper.insert(order);
        QueryWrapper<ShoppBean> qw=new QueryWrapper<>();
        qw.eq("fk_userId",order.getUserId());

        //查询购物车里面现有的商品信息
        List<ShoppBean> shoppList = mapper1.selectList(qw);
        //批量添加订单的明细
        mapper2.addMore(order.getId(),shoppList);
        mapper1.delete(qw);
    }

    @Override
    public IPage<OrderBean> cutByItem(Integer pageNO, Integer userId, String status, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        if (status != null && status.length() != 0) {
            qw.eq("o_status", status);
        }
        if (startDate != null) {
            qw.ge("oderDate", startDate);
        }
        if (endDate != null) {
            qw.le("oderDate", endDate);
        }
        return mapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public OrderBean findById(Integer id) {
        return mapper.findById(id);
    }

    @Override
    public void send(Integer id) {
        OrderBean order = mapper.selectById(id);
        order.setStatus("已完结");
        mapper.updateById(order);
    }

    @Override
    public void compile(Integer id) {
        OrderBean order = mapper.selectById(id);
        order.setStatus("发货中");
        mapper.updateById(order);
    }

    @Override
    public List<OrderBean> findByStatus() {
        QueryWrapper<OrderBean> qw=new QueryWrapper<>();
        qw.eq("o_status","发货中");
        return mapper.selectList(qw);
    }
}
